﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;
using System.Text.RegularExpressions;
using TaoBaoKe.Common;
namespace FormTBK.lib
{
   public class SESuggestion
    {

       public string Get(string url,Encoding encoding,string fatherPattern,string sonPattern) {
           HtmlWeb w = new HtmlWeb();
           string s = "";
           w.OverrideEncoding = encoding;
           w.UseCookies = false;
           w.NeedParse = false;
           w.PreRequest += req;
           HtmlDocument d;
           d = w.Load(url, "get");
           string result = d.Text;
           result = result.Replace("\n", "").Replace("\r", "");
           Match mc = result.Match(fatherPattern);
           if (mc.Success)
           {
               result = mc.Value;
               MatchCollection mcc = result.Matches(sonPattern);
               if (mcc.Count > 0)
               {
                   s = "";
                   for (int i = 0; i < mcc.Count; i++)
                   {
                       s +=(i==0?"":",")+ mcc[i].Groups["key"].Value;
                   }
               }
           }
           return s;
       }
       private bool req(System.Net.HttpWebRequest request)
       {
           request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
           request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
           request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
           request.AutomaticDecompression = DecompressionMethods.GZip;
           Random rnd = new Random();
           request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/"+rnd.Next(100)+".0."+rnd.Next(10000)+".64 Safari/"+rnd.Next(1000)+".31";
           return true;
       }
    }
}
